package eddy.yang;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.*;

public class Clock extends JLabel implements Runnable {

   /**
    * 
    */
   private static final long serialVersionUID = 1L;
   private volatile Thread timer;       // The thread that displays clock
   private SimpleDateFormat formatter;  // Formats the date displayed
   private String lastdate;             // String to hold date displayed
   private Font clockFaceFont;          // Font for number display on clock
   private Date currentDate;            // Used to get date to display
   
   public Clock() {
      //formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
      formatter = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
      currentDate = new Date();
      lastdate = formatter.format(currentDate);
      clockFaceFont = new Font("Serif", Font.PLAIN, 14);
      setFont(clockFaceFont);
      setForeground(Color.blue);
      setText(lastdate);
   }

   
   public void start() {
      timer = new Thread(this);
      timer.start();
  }

  public void stop() {
      timer = null;
  }
  
   
   public void run() {
      Thread me = Thread.currentThread();
      while (timer == me) {
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
          }
          currentDate = new Date();
          lastdate = formatter.format(currentDate);
          setText(lastdate);
      }
      
   }
   
   public static void main(String[] args) {
      JFrame jf = new JFrame();
      Clock cl = new Clock();
      jf.getContentPane().add(cl, BorderLayout.CENTER);
      jf.pack();
      jf.setVisible(true);
      cl.start();
      
   }
   
}
